﻿using System.Data;
using KZ.Utilities;
using Process.Entities;
using KZ.Exception;
using System.Data.SqlClient;

namespace Process.DAL
{
    public class TransactionDAL
    {
        private static TransactionENT CreateTransactionFromReader(IDataReader reader)
        {
            TransactionENT item = new TransactionENT();

            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.TransID.ToString()))) item.TransID = Utility.StringParse(reader[TransactionColumns.TransID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.ProviderID.ToString()))) item.ProviderID = Utility.StringParse(reader[TransactionColumns.ProviderID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.ProductID.ToString()))) item.ProductID = Utility.StringParse(reader[TransactionColumns.ProductID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.ProductPrice.ToString()))) item.ProductPrice = Utility.DecimalParse(reader[TransactionColumns.ProductPrice.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.MinQuantity.ToString()))) item.MinQuantity = Utility.IntParse(reader[TransactionColumns.MinQuantity.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.NextTransQuantity.ToString()))) item.NextTransQuantity = Utility.IntParse(reader[TransactionColumns.NextTransQuantity.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.BeginTransDate.ToString()))) item.BeginTransDate = Utility.DateTimeParse(reader[TransactionColumns.BeginTransDate.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.EndTransDate.ToString()))) item.EndTransDate = Utility.DateTimeParse(reader[TransactionColumns.EndTransDate.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.TransStatus.ToString()))) item.TransStatus = Utility.StringParse(reader[TransactionColumns.TransStatus.ToString()]);

            return item;
        }

        public static TransactionENT GetTransactionByID(string transID)
        {
            try
            {
                TransactionENT transaction = new TransactionENT();

                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, transID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procTransactionGetByID"))
                {
                    while (reader.Read())
                    {
                        transaction = CreateTransactionFromReader(reader);
                    }
                }

                return transaction;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public void InsertTransaction(TransactionENT trans)
        {
            try
            {
                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, trans.TransID);
                DataAccessHelper.AddParameter("ProviderID", SqlDbType.VarChar, trans.ProviderID);
                DataAccessHelper.AddParameter("ProductID", SqlDbType.VarChar, trans.ProductID);
                DataAccessHelper.AddParameter("ProductPrice", SqlDbType.Decimal, trans.ProductPrice);
                DataAccessHelper.AddParameter("MinQuantity", SqlDbType.Int, trans.MinQuantity);
                DataAccessHelper.AddParameter("NextTransQuantity", SqlDbType.Int, trans.NextTransQuantity);
                DataAccessHelper.AddParameter("BeginTransDate", SqlDbType.DateTime, trans.BeginTransDate);
                DataAccessHelper.AddParameter("EndTransDate", SqlDbType.DateTime, trans.EndTransDate);
                DataAccessHelper.AddParameter("TransStatus", SqlDbType.VarChar, trans.TransStatus);

                DataAccessHelper.ExecuteNonQuery("procTransactionInsert");
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình thêm mới giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public bool UpdateTransactionStatus(string transID, TransStatus transStatus)
        {
            try
            {
                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, transID);
                DataAccessHelper.AddParameter("TransStatus", SqlDbType.VarChar, transStatus.ToString());

                if (DataAccessHelper.ExecuteNonQuery("procTransactionUpdateStatus") > 0)
                {
                    return true;
                }
                throw new DataAccessException("Có lỗi trong quá trình cập nhật trạng thái giao dịch.");
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình cập nhật trạng thái giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}